package com.cn.onetrip.activity;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.cn.onetrip.adapter.InfoItemAdapter;
import com.cn.onetrip.adapter.InfoMessageItemAdapter;
import com.cn.onetrip.adapter.MoreItemAdapter;
import com.cn.onetrip.objects.InfosListObj;
import com.cn.onetrip.objects.TipsType;
import com.cn.onetrip.activity.R;
import com.cn.onetrip.untility.ActivityTag;
import com.cn.onetrip.untility.SysApplication;
import com.cn.onetrip.untility.Untility;
import com.cn.onetrip.view.AdView;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

public class InfomationActivity extends Activity implements OnItemClickListener {
	// public static View view;
	private ListView moreList;
	private InfoItemAdapter adapter;
	private InfoMessageItemAdapter adapterMessage;
	private Button quit;
	private TextView titleLeft, titleRight;
	private ImageView leftBar, rightBar;
	private RelativeLayout layoutLeft, layoutRight;
	private List<InfosListObj> tipsList = new ArrayList<InfosListObj>();
	private AdView adView;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// Window w = getWindow();
		// view = w.getDecorView();
		setContentView(R.layout.activity_info);
		layoutLeft = (RelativeLayout) findViewById(R.id.InfoLayoutTilteLeft);
		layoutRight = (RelativeLayout) findViewById(R.id.InfoLayoutTilteRight);
		leftBar = (ImageView) findViewById(R.id.InfoTitleBarLeft);
		rightBar = (ImageView) findViewById(R.id.InfoTitleBarRight);
		titleLeft = (TextView) findViewById(R.id.InfoTitleLeft);
		titleRight = (TextView) findViewById(R.id.InfoTitleRight);
		moreList = (ListView) findViewById(R.id.InfoList);
		adView = (AdView) findViewById(R.id.ad_view);

		adapter = new InfoItemAdapter(InfomationActivity.this, moreList,
				handler);
		adapterMessage = new InfoMessageItemAdapter(InfomationActivity.this,
				moreList, handler);
		// moreList.setAdapter(adapterMessage);
		moreList.setAdapter(adapter);
		moreList.setOnItemClickListener(this);
		quit = (Button) findViewById(R.id.quit);
		quit.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				finish();
				overridePendingTransition(R.anim.push_left_translate,
						R.anim.pull_right_translate);
			}
		});
		titleLeft.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				if (leftBar.getVisibility() != View.VISIBLE) {
					// adapter.clean();
					adapterMessage.clean();
					moreList.setAdapter(adapterMessage);
					Message msg = Message.obtain();
					msg.what = 0;
					handler.sendMessage(msg);
				}
			}
		});
		titleRight.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				if (rightBar.getVisibility() != View.VISIBLE) {
					// adapter.clean();
					adapterMessage.clean();
					moreList.setAdapter(adapter);
					Message msg = Message.obtain();
					msg.what = 1;
					handler.sendMessage(msg);
				}
			}
		});

		Button more = (Button) findViewById(R.id.more_info);
		more.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent intent = new Intent(getApplicationContext(),
						MoreGridActivity.class);
				startActivity(intent);
				overridePendingTransition(R.anim.push_right_translate,
						R.anim.pull_left_translate);
			}
		});

		// loadMessage();
		loadDate();
	}

	@Override
	protected void onResume() {
		startClick(true);
		adView.startTimer();
		super.onResume();
	}

	@Override
	protected void onRestart() {

		super.onRestart();
	}

	@Override
	protected void onPause() {
		adView.stopTimer();
		super.onPause();
	}

	private void startClick(boolean isClose) {
		titleLeft.setEnabled(isClose);
		titleRight.setEnabled(isClose);
	}

	public void loadDate() {
		// adapter.clean();
		// // tipsList.clear();
		tipsList = SysApplication.infosMapList.get("tips");
		// if (tipsList == null) {
		// return;
		// }
		// for (int i = 0; i < tipsList.size(); i++) {
		// InfosListObj infoObj = tipsList.get(i);
		// adapter.addInfoTips(infoObj.tip_type_cd, infoObj.title);
		// }
		//
		// adapter.addInfoTips(TipsType.travels,
		// getResources().getString(R.string.info_travels));
	}

	public void loadMessage() {
		adapterMessage.clean();
		// tipsList.clear();
		tipsList = SysApplication.infosMapList.get("news");
		if (tipsList == null) {
			return;
		}
		for (int i = 0; i < tipsList.size(); i++) {
			InfosListObj infoObj = tipsList.get(i);
			adapterMessage.addInfoNews(infoObj.title, infoObj.time,
					infoObj.source);
		}
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		int typeID = ((InfosListObj) adapter.getItem(arg2)).tip_type_cd;

		if (typeID == TipsType.travels) {
			Intent intent = new Intent(getApplicationContext(),
					TravelActivity.class);
			startActivity(intent);
			overridePendingTransition(R.anim.push_right_translate,
					R.anim.pull_left_translate);
			return;
		}

		startClick(false);
		Intent intent = new Intent(InfomationActivity.this,
				TravelDetailActivity.class);
		Bundle bundle = new Bundle();
		int drawableID = 0;
		if (adapterMessage.getCount() > 0) {
			bundle.putInt("title_image", R.drawable.infomation_title);
		} else {
			switch (typeID) {
			case TipsType.tickets:
				drawableID = getResources().getIdentifier("title_ticket",
						"drawable", getApplicationContext().getPackageName());
				break;
			case TipsType.traffic:
				drawableID = getResources().getIdentifier("title_traffic",
						"drawable", getApplicationContext().getPackageName());
				break;
			case TipsType.todo:
				drawableID = getResources().getIdentifier("title_play",
						"drawable", getApplicationContext().getPackageName());
				break;
			case TipsType.consume:
				drawableID = getResources().getIdentifier("title_consume",
						"drawable", getApplicationContext().getPackageName());
				break;
			case TipsType.arm:
				drawableID = getResources().getIdentifier("title_arm",
						"drawable", getApplicationContext().getPackageName());
				break;
			case TipsType.feast:
				drawableID = getResources().getIdentifier("title_feast",
						"drawable", getApplicationContext().getPackageName());
				break;
			case TipsType.way:
				drawableID = getResources().getIdentifier("title_the_way",
						"drawable", getApplicationContext().getPackageName());
				break;
			case TipsType.alert:
				drawableID = getResources().getIdentifier("title_alert",
						"drawable", getApplicationContext().getPackageName());
				break;
			case TipsType.tips:
				drawableID = getResources().getIdentifier("title_tip",
						"drawable", getApplicationContext().getPackageName());
				break;

			default:
				break;
			}
			bundle.putInt("title_image", drawableID);
		}
		// bundle.putInt("title_image", R.drawable.title_detail);
		bundle.putBoolean("Left", adapterMessage.getCount() > 0 ? true : false);
		bundle.putString("module", ActivityTag.Info);
		bundle.putInt("itemId", arg2);
		bundle.putString("title", tipsList.get(arg2).title);
		bundle.putString("time", tipsList.get(arg2).time);
		bundle.putString("source", tipsList.get(arg2).source);
		intent.putExtras(bundle);
		startActivity(intent);
		overridePendingTransition(R.anim.push_right_translate,
				R.anim.pull_left_translate);
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
			// view.startAnimation(Untility.rQuest1Animation);
			// GroupActivity.changeFirstView();
			finish();
			overridePendingTransition(R.anim.push_left_translate,
					R.anim.pull_right_translate);
		}
		return super.onKeyDown(keyCode, event);
	}

	public Handler handler = new Handler() {
		public void dispatchMessage(android.os.Message msg) {
			switch (msg.what) {
			case 0:
				titleLeft.setTextColor(getResources().getColor(
						R.color.info_title_select));
				leftBar.setVisibility(View.VISIBLE);
				titleRight.setTextColor(Color.WHITE);
				rightBar.setVisibility(View.INVISIBLE);
				loadDate();
				loadMessage();
				adapterMessage.notifyDataSetChanged();
				break;
			case 1:
				titleLeft.setTextColor(Color.WHITE);
				leftBar.setVisibility(View.INVISIBLE);
				titleRight.setTextColor(getResources().getColor(
						R.color.info_title_select));
				rightBar.setVisibility(View.VISIBLE);
				// loadMessage();
				// loadDate();
				adapter.notifyDataSetChanged();
				break;
			default:
				break;
			}
		};
	};

}
